package com.yanze.schoolinformation.viewModule.relax;

import com.yanze.schoolinformation.app.App;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.GankDataBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.cache.ACache;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/4/28.
 */

public class WelfareViewModel {
    private BaseFragment activity;
    private final ACache mACache;
    private WelfareNavigator navigator;
    private int mPage = 1;
    private ArrayList<String> imgList = new ArrayList<>();
    private ArrayList<String> imageTitleList = new ArrayList<>();

    public void setNavigator(WelfareNavigator navigator) {
        this.navigator = navigator;
    }

    public void onDestroy() {
        navigator = null;
        activity = null;
    }

    public WelfareViewModel(BaseFragment activity) {
        this.activity = activity;
        mACache = ACache.get(App.getMyApplication());
    }

    public void loadWelfareData() {
        DataReqo.getInstant().getGankIoData(C.url.API_GANKIO, "福利", C.count.count, mPage)
                .subscribe(new DeforObserver<GankDataBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        activity.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(GankDataBean gankIoDataBean) {
                        navigator.showLoadSuccessView();
                        if (mPage == 1) {
                            if (gankIoDataBean != null
                                    && gankIoDataBean.getResults() != null
                                    && gankIoDataBean.getResults().size() > 0) {
                                imgList.clear();
                                for (int i = 0; i < gankIoDataBean.getResults().size(); i++) {
                                    imgList.add(gankIoDataBean.getResults().get(i).getUrl());
                                    imageTitleList.add(gankIoDataBean.getResults().get(i).getDesc());
                                }
                                navigator.setImageList(imgList, imageTitleList);
                                navigator.showAdapterView(gankIoDataBean);
                            } else {
                                navigator.showLoadFailedView();
                            }
                        } else {
                            if (gankIoDataBean != null && gankIoDataBean.getResults() != null && gankIoDataBean.getResults().size() > 0) {
                                navigator.refreshAdapter(gankIoDataBean);
                                for (int i = 0; i < gankIoDataBean.getResults().size(); i++) {
                                    imgList.add(gankIoDataBean.getResults().get(i).getUrl());
                                    imageTitleList.add(gankIoDataBean.getResults().get(i).getDesc());
                                }
                                navigator.setImageList(imgList, imageTitleList);
                            } else {
                                navigator.showListNoMoreLoading();
                            }
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        navigator.showLoadFailedView();
                        if (mPage > 1) {
                            mPage--;
                        }
                    }
                });
    }

    public int getPage() {
        return mPage;
    }

    public void setPage(int mPage) {
        this.mPage = mPage;
    }
}
